import GameData_blxf from "../GameData/GameData";
import { CsvManager_blxf } from "../Manager/CsvManager";
import { EnumManager_blxf } from "../Manager/EnumManager";
import AudioModule_blxf from "../Module/AudioModule";
import { LoadModule_blxf } from "../Module/LoadModule";


const {ccclass, property} = cc._decorator;

/** 游戏补货 */

@ccclass
export default class AddShopAnimation_blxf extends cc.Component {

   @property({ type: cc.Node, tooltip: '图片item' })
   shopImgItem: cc.Node = null;

   @property({ type: cc.Node, tooltip: '图片盒子' })
   shopImgBox: cc.Node = null;


   @property({ tooltip: '传入参数' })
   _options: any = null;

   @property({ tooltip: '最多图片' })
   _maxNum: number = 15;

   @property({ tooltip: '图片列表' })
   _imgList: any[] = [];

   _isLoadOk: boolean = false;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
        this.shopImgItem.opacity = 0;
    }

    initData_blxf ( options: any ) {
        this._options = options;

        this.onInitImgList_blxf();
    }

    /** 初始化已解锁商品列表 */
    onInitImgList_blxf () {
        let { CSV_SHOP } = EnumManager_blxf.instance.getGameCsvType();
        let _cfgShopData: any[] = CsvManager_blxf.instance.getCsvDataByName_blxf( CSV_SHOP );
        // 过滤鞭炮
        _cfgShopData = _cfgShopData.filter( item => item.ShopId != '100001' ); 
        let _shopList: any[] = GameData_blxf.instance.getShopList_blxf();

        let _shopDataList: any[] = []; 
        _cfgShopData.forEach( cfgItem =>{
            let { ShopId, AtlasName, ImgName } = cfgItem;

            let _shopData = _shopList.find( item => item.shopId == ShopId );
            _shopData && _shopDataList.push({ path: AtlasName, imgName: ImgName });
        })

        this._imgList = [ ];
        for ( let i = 0; i < this._maxNum; i++ ) {
            this.onRandomImgData_blxf( _shopDataList );
        }
    }

    /** 随机商品 */
    onRandomImgData_blxf ( _shopDataList: any[] ) {
        _shopDataList.sort((a, b) => Math.random() - 0.5 );

        this._imgList.push( _shopDataList[0] );
    }

    update ( dt: number ) {
        !this._isLoadOk && this.loadImgList_blxf();
    }

    loadImgList_blxf () {
        let _loadLen: number = this.shopImgBox.childrenCount;
        let _isLoadOk: boolean = _loadLen >= this._imgList.length;
        if ( _isLoadOk ) return this._isLoadOk = true;

        this.loadImgAnimation_blxf( _loadLen );
    }

    loadImgAnimation_blxf ( index: number ) {
        let _imgNode: cc.Node = LoadModule_blxf.instance.LoadNode_blxf({
            prefabNode: this.shopImgItem, parentNode: this.shopImgBox
        })
        if ( !_imgNode ) return;

        _imgNode.opacity = 255;

        let _imgSpr: cc.Sprite = _imgNode.getComponent( cc.Sprite );
        if ( !_imgSpr ) return;

        let { path, imgName } = this._imgList[ index ];
        imgName && LoadModule_blxf.instance.LoadImg_blxf({
            path, imgName, imgSpr: _imgSpr
        })

        let { toP, btnShopNum, okCB } = this._options;
        let _isMax: boolean = index + 1 == this._maxNum;
        let _shopNum: number = Math.ceil( btnShopNum * (index + 1) / this._maxNum );

        let _byTime: number = 0.6, _toTime: number = 1, _scale: number = 0.8;
        let getR = ( n: number ) => { return Math.floor(Math.random() * n) };
        let num1 = getR(240) - 120;
        let num2 = getR(240) - 120;
        let num3 = getR(200) / 1000;

        cc.tween(_imgNode)
            .delay(num3)
            .by( _byTime, {x: num1, y: num2}, cc.easeSineOut() )
            .to( _toTime, {x: toP.x, y: toP.y, scale: _scale }, cc.easeSineInOut())
            .call(()=>{
                index <= 9 && AudioModule_blxf.instance.playShopOkAudio_xft( index + 1 );

                okCB && okCB( _shopNum );

                _imgNode.destroy();

                _isMax && this.onClose_blxf();
            })
            .start();
    }

    onClose_blxf () {
        this.node.destroy();
    }
}
